package com.linrui.org.controller;

import com.linrui.basic.utils.AjaxResult;
import com.linrui.basic.utils.PageList;
import com.linrui.org.domain.Department;
import com.linrui.org.query.DepartmentQuery;
import com.linrui.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 部门管理操作
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门管理controller",description = "部门管理controller")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增和修改   put
     * 对于ajax请求来说,后端需要告知前端请求成功与否,并且如果失败,还要告诉失败原因
     * 封装一个对象AjaxResult {success:true,message:"失败原因"}
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "保存部门")
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId() != null){
                departmentService.update(department);
            }else{
                departmentService.add(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

    // 删除单条  delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门")
    public AjaxResult removeById(@ApiParam(required = true) @PathVariable("id")Long id){
        try {
            departmentService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

    /**
     * 查询单条数据  get
     * 对于前端ajax请求来说,不管是增删改还是查询,都要告知前端操作是否成功 所以查询方法也应该返回AjaxResult
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询部门详情")
    public AjaxResult getById(@ApiParam(required = true) @PathVariable("id")Long id){
        try {
            Department department = departmentService.getById(id);
            return AjaxResult.me().setResult(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    // 查询所有    get
    @GetMapping
    @ApiOperation(value = "查询所有部门")
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return AjaxResult.me().setResult(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    // 分页查询  post
    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> pageList = departmentService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    // 批量操作 @TODO
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult batchremove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }
    @GetMapping("/tree")
    @ApiOperation(value = "getTree")
    public AjaxResult getTree() {
        try {
            List<Department> tree = departmentService.getTree();
            return AjaxResult.me().setResult(tree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!" + e.getMessage());
        }
    }
}
